import {createAsyncThunk, createSlice} from "@reduxjs/toolkit";


export const fetchChannelsList = createAsyncThunk("fetchChannels", async () => {
    const result = await fetch("http://geek.itheima.net/v1_0/channels")
    const {data: {channels}} = await result.json()
    return channels
})

const channelsStore = createSlice({
    name: "channelsStore",
    initialState: {
        channels: []
    },
    reducers: {},
    extraReducers(builder) {
        builder.addCase(fetchChannelsList.pending, (state, action) => {
        }).addCase(fetchChannelsList.fulfilled, (state, action) => {
            state.channels = action.payload
        }).addCase(fetchChannelsList.rejected, (state, action) => {

        })
    }
})


export const channels = state => state.channelsStore.channels
export default channelsStore.reducer
